package com.example.inboxdemo.controller;

import com.example.inboxdemo.component.SseEmitterServer;
import com.example.inboxdemo.pojo.entity.PublishDTO;
import com.example.inboxdemo.pojo.entity.Result;
import com.example.inboxdemo.service.IMsgSendService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;


@Slf4j
@RequiredArgsConstructor
@RestController
public class InboxController {

    private final SseEmitterServer sseEmitterServer;
    private final IMsgSendService msgSendService;

    /**
     * 模拟用户上线，订阅自己的站内信，服务端返回SseEmitter对象
     *
     * @return SseEmitter对象
     */
    @GetMapping("/online/{userId}")
    public SseEmitter online(@PathVariable String userId) throws IOException {
        return sseEmitterServer.connect(userId);
    }

    /**
     * 模拟消息发布接口
     *
     * @param publishDTO 消息体
     * @return 消息发送结果
     */
    @PostMapping("/publish")
    public Result<Void> publish(@RequestBody PublishDTO publishDTO) {
        msgSendService.publish(publishDTO);
        return Result.success();
    }
}
